<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/views/include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="/views/include/nocachehead.jsp"%>
<c:set var="categories" scope="page" value="${requestScope.CATEGORIES}"></c:set>
<c:set var="article" scope="page" value="${requestScope.ARTICLE}"></c:set>
<title>修改日志--${article.title } </title>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/plugin/ckeditor/ckeditor.js"></script>
<style type="text/css">
</style>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/validator/modifyArticleForm.js"></script>
<script type="text/javascript">
//=========================html替换类============  
function replacehtml()  
{  
    var str="";//注入字段  
  
    function setStr()  
    {  
        this.str=arguments[0];  
    }  
    function getStr()  
    {  
        return this.str;  
    }  
      
    //自定义html替换  
    function replace()  
    {  
        if(arguments.length>0)  
        {  
            this.str=arguments[0];  
        }  
        this.str=this.str.replace(/<font.*?<\/font>/g,"");//font标签  
        this.str=this.str.replace(/<script .*?<\/script>/ig,""); //script标签  
        this.str=this.str.replace(/<(a|img)/ig, "@@@$1"); //img标签  
        this.str=this.str.replace(/<[^>a]*>/ig, ""); //a标签  
        this.str=this.str.replace(/@@@(a|img)/ig, "<$1"); //a|img  
        this.str=this.str.replace(/<br>/ig,"");//换行  
        return this.str;  
    }  
      
    //替换所有html标签  
    function replaceAll()  
    {  
        if(arguments.length>0)  
        {  
            this.str=arguments[0];  
        }  
        this.str=this.str.replace(/<[^>]*>/g, "");  
        this.str=this.str.replace(/\r\n/g, "<br />");  
        return this.str;  
    }  
      
    var o={};  
    o.setStr=setStr;  
    o.getStr=getStr;  
    o.replace=replace;  
    o.replaceAll=replaceAll;  
    return o;  
}  
	var ajaxAddCategoryUrl = "${pageContext.request.contextPath}/article/category/ajaxadd.html";

	function ajaxAddCategoryRequest() {
		var ajaxAddCategoryTable = "<table><tr><td colspan='2'><font id='addCategoryErrorMessage' color='red' style='display:none;'></font></td></tr><tr><td>分类名称:</td><td><input type='text' name='categoryName' id='addCategoryName'/></td></tr><tr><td colspan='2' align='right'><input type='button' onclick='ajaxAddCategoryExecute()' value='提交'>&nbsp;<input type='button' onclick='ajaxAddCategoryCancel()' value='取消'>&nbsp;&nbsp;</td></tr></table>";
		jMessageBox(ajaxAddCategoryTable, '增加日志分类');
	}

	function ajaxAddCategoryExecute() {
		var addCategoryErrorMessage = document
				.getElementById("addCategoryErrorMessage");
		var addCategoryName = document.getElementById("addCategoryName").value;
		if (addCategoryName == '') {
			addCategoryErrorMessage.innerHTML = "你为难我了，输入点东西吧";
			addCategoryErrorMessage.style.display = "";
		} else if (addCategoryName.length > 40) {
			addCategoryErrorMessage.innerHTML = "你让我为难了，输入得太多了点吧，最多也就40字啊";
			addCategoryErrorMessage.style.display = "";
		} else {
			$
					.ajax({
						url : ajaxAddCategoryUrl,
						data : "categoryName=" + addCategoryName,
						type : "POST",
						dataType : "json",
						contentType : "application/x-www-form-urlencoded; charset=utf-8",
						success : function(msg) {
							if (msg != null) {
								var category = document
										.getElementById("category");
								category.options.length = 0;
								for(var i=0;i<msg.length;i++){
									var categoryItem=msg[i];
									jsAddItemToSelect(category,categoryItem.categoryName,categoryItem.id);
								}
								$.alerts._hide();
							} else {
								var addCategoryErrorMessage = document
										.getElementById("addCategoryErrorMessage");
								addCategoryErrorMessage.innerHTML = "插入失败，该分类名已经存在或者您未登陆";
								addCategoryErrorMessage.style.display = "";
							}
						}
					});
		}
	}

	function ajaxAddCategoryCancel() {
		$.alerts._hide();
	}

	function checkArticleTile() {
		var title = document.getElementById("title").value;
		//var titleErrorMessage = document.getElementById("titleErrorMessage");
		if (title == "") {
			/* titleErrorMessage.style.display = "";
			titleErrorMessage.innerHTML = "还是写上标题吧"; */
			return false;
		} else if (title.length > 120) {
			/* titleErrorMessage.style.display = "";
			titleErrorMessage.innerHTML = "标题太长啦，最多也能是120个字"; */
			return false;
		} else {
			//titleErrorMessage.style.display = "none";
			return true;
		}

	}

	function modifyArticleSubmit() {
		var modifyArticleForm = document.getElementById("modifyArticleForm");
		//var title=document.getElementById("title");
		if (checkArticleTile() == true) {
			//document.getElementById("modifyArticleForm").submit();
			var a=new replacehtml();
			//content
			//var content=document.getElementById("content").value;
			KE.util.setData("content");
			var content=document.getElementById("content").value;
			//alert(content);
			a.setStr(content);  
			a.replaceAll();  
			//document.write("测试1：<br/>"+a.getStr()+"<br/>"); 
			var digestInput=document.getElementById("digest");
			var digest=a.getStr();
			if(digest.length>400){
				digest=digest.substring(0,400);
			}
			if(digest.containSensitiveWords()){
				return false;
			}
			else{
				if(digest.length>180){
					digest=digest.substring(0,180);
				}
				digestInput.value=digest;
				$("#modifyArticleForm").submit();
			}
		}
	}
	
	function setCategoryName(categoryNameValue){
		//alert(categoryNameValue);
		var categoryName=document.getElementById("categoryName");
		categoryName.value=categoryNameValue;
	//alert(categoryName.value);
	}
	
</script>
<script charset="utf-8" src="/skyline/plugin/kindeditor/kindeditor.js"></script>
		<script type="text/javascript">
			KE.show({
				id : 'content',
				resizeMode : 1,
				allowPreviewEmoticons : true,
				allowUpload : true,
				items : [
				'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline',
				'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'emoticons', 'image', 'link'],
				afterCreate : function(id) {
					KE.event.ctrl(document, 13, function() {
						KE.sync(id);
						modifyArticleSubmit();
					});
					KE.event.ctrl(KE.g[id].iframeDoc, 13, function() {
						KE.sync(id);
						modifyArticleSubmit();
					});
				}
			});

		</script>
<link href="${pageContext.request.contextPath}/css/base.css"
	rel="stylesheet" type="text/css" />
	<style type="text/css">
	#content{
	width:640px;
	height:400px;
	visibility:hidden;
	}</style>
</head>
<body>
<c:out value="${requestScope.errMsg}" escapeXml="false"/>
<form id="modifyArticleForm"
	action="${pageContext.request.contextPath}/article/modify/${article.id}.html"
	method="post">
<table width="640px">
	<tr>
		<td>标题:</td>
		<td><input id="title" class="Input" name="title" type="text"
			size="64" style="width: 580px; height: 20px;" value="${article.title }" />
		</td>
	</tr>
	<tr>
		<td colspan="2"><textarea id="content" name="content" >${article.content }</textarea>
		<input type="hidden" name="digest" id="digest"/>
		</td>
	</tr>
	<tr>
		<td>分类:<input id="categoryName" type="hidden" name="categoryName" value="${categories[0].categoryName}"></td>
		<td><select name="categoryId" id="category" onchange="setCategoryName(this.options[this.options.selectedIndex].text)">
			<c:forEach var="category" items="${categories}">
				<option value="${category.id}" <c:choose>
					<c:when test="${category.id==article.categoryId}">selected="selected"</c:when>
				</c:choose>
				>${category.categoryName}</option>
				
			</c:forEach>
		</select> <input type="button" value="增加分类" onclick="ajaxAddCategoryRequest()"></td>
	</tr>
	<tr>
		<td>权限:</td>
		<td><select id="authority" name="authority">
			<option value="0" <c:choose><c:when test="${category.authority==0}">selected="selected"</c:when></c:choose> >公开</option>
			<option value="10" <c:choose><c:when test="${category.authority==10}">selected="selected"</c:when></c:choose> >仅粉丝</option>
			<option value="20" <c:choose><c:when test="${category.authority==20}">selected="selected"</c:when></c:choose> >仅自己</option>
		</select>
		<input type="hidden" name="submitToken" value="<skyline:sessionAttribute key="SESSION_SUBMIT_TOKEN" />"/> 
		</td>
	</tr>
	<tr>
		<td colspan="2" align="right"><input class="btn_ok" type="button"
			value="提交" onclick="modifyArticleSubmit()"> <input
			class="btn_cancel" type="reset" id="reset" value="取消"></td>
	</tr>
</table>
</form>
</body>
</html>